package com.rocksdata.mc.ui.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.StyleRes;
import android.view.View;
import android.widget.TextView;

import com.ctz.commonframe.utils.DateUtil;
import com.ctz.commonframe.utils.DensityUtil;
import com.rocksdata.mc.ui.activity.HomeworkDetailActivity;
import com.rocksdata.mc.ui.activity.HomeworkReleaseActivity;
import com.rocksdata.mc.R;
import com.rocksdata.mc.business.TimetableManager;
import com.rocksdata.mc.business.UserInfoManager;
import com.rocksdata.mc.entity.CourseInfo;
import com.rocksdata.mc.entity.UserInfo;
import com.rocksdata.mc.ui.activity.PreviewDetailActivity;
import com.rocksdata.mc.ui.activity.PreviewReleaseActivity;
import com.rocksdata.mc.ui.activity.UndevelopedActivity;

/**
 * Created by 陈堂正 on 2017/2/14.
 */

public class CourseDetailDialog extends AlertDialog implements View.OnClickListener{
    private TextView tvDayOfWeek, tvDate;
    private TextView tvCourseName, tvTeacherName, tvCourseTime, tvLocation;
    private TextView tvMenu1, tvMenu2, tvMenu3;
    private CourseInfo info;
    private long dateMillis;
    private int userType;
    private Context context;

    public CourseDetailDialog(Context context, CourseInfo info, long dateMillis) {
        this(context, R.style.CustomDialog);
        this.info = info;
        this.dateMillis = dateMillis;
        this.context = context;
//        TimetableManager.calculateDateForCourse(info, dateMillis);
    }

    protected CourseDetailDialog(Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_course_detail);
        setCancelable(false);
        findViewById(R.id.iv_dialog_cancel).setOnClickListener(this);
        tvDayOfWeek = (TextView) findViewById(R.id.tv_course_day_of_week);
        tvDate = (TextView) findViewById(R.id.tv_course_date);
        tvCourseName = (TextView) findViewById(R.id.tv_course_name);
        tvTeacherName = (TextView) findViewById(R.id.tv_teacher_name);
        tvCourseTime = (TextView) findViewById(R.id.tv_course_time);
        tvLocation = (TextView) findViewById(R.id.tv_course_location);
        tvMenu1 = (TextView) findViewById(R.id.tv_course_menu1);
        tvMenu2 = (TextView) findViewById(R.id.tv_course_menu2);
        tvMenu3 = (TextView) findViewById(R.id.tv_course_menu3);

        tvDayOfWeek.setText(DateUtil.getDayOfWeek(dateMillis));
        tvDate.setText(DateUtil.millis2String(dateMillis, DateUtil.DATE_FORMAT_YYYY_MM_DD_CN));
        tvCourseName.setText(info.getCourseName());

        String index;
//        if(info.duration == 1){
//            index = info.getLesson() + "";
//        }else{
//            index = info.getLesson() + "-" + (info.getLesson() + info.duration - 1);
//        }
//        tvCourseTime.setText(DateUtil.millis2String(info.dateMillis, DateUtil.TIME_FORMAT_HH_MM));
        tvLocation.setText(info.getLocation());

        tvTeacherName.setCompoundDrawablePadding(DensityUtil.dip2px(context, 10));
        userType = UserInfoManager.getCurrentUserType();
        if(userType == UserInfo.USER_TYPE_STUDENT){
            tvTeacherName.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.ic_course_detail_teacher, 0, 0, 0);
            tvTeacherName.setText(info.getStaffName());
            tvMenu1.setText("查看预习");
            tvMenu2.setText("查看作业");
            tvMenu3.setText("发表提问");
        }else if(userType == UserInfo.USER_TYPE_TEACHER){
            tvTeacherName.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.ic_classroom, 0, 0, 0);
            tvTeacherName.setText(info.getClassName());
            tvMenu1.setText("发布预习");
            tvMenu2.setText("发布作业");
            tvMenu3.setVisibility(View.INVISIBLE);
        }

        tvMenu1.setOnClickListener(this);
        tvMenu2.setOnClickListener(this);
        tvMenu3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_dialog_cancel:
                dismiss();
                break;
            case R.id.tv_course_menu1:
                context.startActivity(
                        new Intent(context, userType == UserInfo.USER_TYPE_STUDENT
                                ? PreviewDetailActivity.class
                                : PreviewReleaseActivity.class)
                                .putExtra("course_info", info));
                break;
            case R.id.tv_course_menu2:
                context.startActivity(
                        new Intent(context, userType == UserInfo.USER_TYPE_STUDENT
                                ? HomeworkDetailActivity.class
                                : HomeworkReleaseActivity.class)
                                .putExtra("course_info", info));
                break;
            case R.id.tv_course_menu3:
                context.startActivity(
                        new Intent(context, UndevelopedActivity.class)
                                .putExtra("app_name", "发布提问"));
                break;

        }
    }
}
